package sg4c;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import sg4c.frontend.GraphicsFrontend;
import java.io.IOException;
import sg4c.frontend.GraphicPanel;
import javax.swing.*;
import sg4c.frontend.CapturingGraphicFrontend;
import sg4c.frontend.ImageCaptureGraphicFrontend;
import sg4c.frontend.PlaybackGraphicFrontend;

public class Main {

    static class Listener implements WindowListener {

        GraphicsFrontend applet;

        public Listener(GraphicsFrontend applet) {
            this.applet = applet;
        }

        public void windowOpened(WindowEvent e) {
            applet.start();
        }

        public void windowClosing(WindowEvent e) {
            applet.stop();
            System.exit(1);
        }

        public void windowClosed(WindowEvent e) {
        }

        public void windowIconified(WindowEvent e) {
        }

        public void windowDeiconified(WindowEvent e) {
        }

        public void windowActivated(WindowEvent e) {
        }

        public void windowDeactivated(WindowEvent e) {
        }
    }

    public static void main(String[] args) throws IOException {
        GraphicsFrontend applet;
        //experimenting with special modes
        if (args.length >= 3 && args[1].equals("-images")) {
            applet = new ImageCaptureGraphicFrontend(args[0], args[2]);
        } else if (args.length >= 3 && args[1].equals("-capture")) {
            System.out.println("Capture mode");
            applet = new CapturingGraphicFrontend(args[0], args[2]);
        } else if (args.length >= 2 && args[1].equals("-playback")) {
            System.out.println("Playback mode");
            applet = new PlaybackGraphicFrontend(args[0]);
        } else {
            applet = new GraphicsFrontend(args[0]);
        }

        applet.init();

        GraphicPanel panel = new GraphicPanel(applet);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.addWindowListener(new Listener(applet));


        frame.add(panel);

        // Frame.setUndecorated(true);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
    }
}
